package realm;


import cn.hutool.core.util.StrUtil;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * 自定义Realm
 *
 * @author lwh
 */
public class CustomerRealm extends AuthorizingRealm {

    /**
     * 认证
     * @param token 用户信息和凭证信息
     * @return 认证信息
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        String principal = String.valueOf(token.getPrincipal());

        if (StrUtil.isBlank(principal)) {
            return null;
        }

        if ("zhangsan".equals(principal)) {
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo("zhangsan", "123", this.getName());
            return info;
        }

        return null;
    }

    /**
     * 授权
     * @param principals 凭证信息
     * @return 凭证信息
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

        return null;
    }


}
